package com.itheima.controller;


import cn.itheima.health.entity.QueryPageBean;
import cn.itheima.health.entity.Result;
import cn.itheima.health.resources.MessageConstant;
import cn.itheima.health.service.PermissionService;
import com.itheima.pojo.Permission;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map map = permissionService.findPage(queryPageBean);
            return new Result(true,"查询权限信息成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限信息失败");
        }
    }

    @RequestMapping("/add.do")
    public Result add(@RequestBody Permission permission){

        try {
            permissionService.add(permission);
            return new Result(true, "新增权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"新增权限失败");
        }

    }

    @RequestMapping("/edit.do")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            return new Result(true, "编辑权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"编辑权限失败");
        }
    }

    @RequestMapping("/deletePermissionById.do")
    public Result deletePermissionById(@RequestParam Integer id){
        try {
            permissionService.deletePermissionById(id);
            return new Result(true,"删除权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除权限失败");
        }
    }

    @RequestMapping("/findPermissionAll.do")
    public Result findPermissionAll(){
        try {
            List<Permission> list = permissionService.findPermissionAll();
            return new Result(true,"查询权限信息成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限信息失败");
        }

    }
    @RequestMapping("/findPermissionIdsByRoleId.do")
    public Result findPermissionIdsByRoleId(@RequestParam("id")Integer id){
        try {
            Map map = permissionService.findPermissionIdsByRoleId(id);
            return new Result(true,"查询权限信息成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限信息失败");
        }
    }
}
